<?php

/**
 * 夏日葵电商系统
 * http://www.xiarikui.com
 */

namespace Admin\Controller;

use Think\Controller;
use Admin\Model\UpgradeModel;

class BaseController extends Controller {

    /**
     * 析构函数
     */
    function __construct() {
        parent::__construct();
        $upgradeModel = new UpgradeModel();
        //$upgradeMsg = $upgradeModel->checkVersion(); //升级包消息        
        $upgradeMsg = '';
        $this->assign('upgradeMsg', $upgradeMsg);
        $navigate_admin = navigate_admin();
        $this->assign('navigate_admin', $navigate_admin);
        //tpversion();
    }

    /**
     * 初始化操作
     */
    public function _initialize() {
        $this->assign('action', ACTION_NAME);
        if (in_array(ACTION_NAME, array('login', 'logout', 'vertify')) || in_array(CONTROLLER_NAME, array('Ueditor', 'Uploadify', 'Public'))) {
            //return;
        } else {
            if (session('admin_id') > 0) {
                $this->checkPriv(); //检查管理员菜单操作权限
            } else {
                $this->redirect('/Admin');
                exit();
//                $this->error('请先登陆', U('Admin/Admin/login'), 1);
            }
        }
        $this->publicAssign();
    }

    /**
     * 保存公告变量
     */
    public function publicAssign() {
        $arr = array();
        $configRs = M('config')->select();
        foreach ($configRs as $k => $v) {
            $arr[$v['inc_type'] . '_' . $v['name']] = $v['value'];
        }
        $this->assign('xiarikui_config', $arr);
    }

    /**
     * 检查权限
     */
    public function checkPriv() {
        $ctl = CONTROLLER_NAME;
        $act = ACTION_NAME;
        $actRs = session('act_list');
        $uneedCheck = array('login', 'logout', 'vertifyHandle', 'vertify', 'imageUp', 'upload', 'loginTask');
        if ($ctl == 'Index' || $actRs == 'all') {
            return true;
        } elseif (strpos('ajax', $act) || in_array($act, $uneedCheck)) {
            return true;
        } else {
            $right = M('sys_menu')->where("id in ($actRs)")->getField('right', true);
            foreach ($right as $val) {
                $right .= $val . ',';
            }
            $right = explode(',', $right);
            if (!in_array($ctl . 'Controller@' . $act, $right)) {
                $this->error('您没有操作权限,请联系超级管理员分配权限', U('Admin/Index/welcome'));
            }
        }
    }


    /**
     * 初始化编辑器链接
     */
    protected function initEditor($savePath) {
        $this->assign("URL_upload", U('Admin/Ueditor/imageUp', array('savepath' => $savePath)));
        $this->assign("URL_fileUp", U('Admin/Ueditor/fileUp', array('savepath' => $savePath)));
        $this->assign("URL_scrawlUp", U('Admin/Ueditor/scrawlUp', array('savepath' => $savePath)));
        $this->assign("URL_getRemoteImage", U('Admin/Ueditor/getRemoteImage', array('savepath' => $savePath)));
        $this->assign("URL_imageManager", U('Admin/Ueditor/imageManager', array('savepath' => $savePath)));
        $this->assign("URL_imageUp", U('Admin/Ueditor/imageUp', array('savepath' => $savePath)));
        $this->assign("URL_getMovie", U('Admin/Ueditor/getMovie', array('savepath' => $savePath)));
        $this->assign("URL_Home", "");
    }

}
